package com.es.config;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem.NullsOrderType;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;

import redis.clients.jedis.JedisPoolConfig;
/**
 * https://blog.csdn.net/didi7696/article/details/79257944
 * @author Administrator
 *
 */
//@Configuration
public class RedisClusterConfig {
	/*	@Autowired
	ClusterConfigurationProperties clusterConfigurationProperties;*/
	@Autowired
	private Environment env;
	
	//@Bean("redisConnectionFactory")
	public RedisConnectionFactory getRedisConnectionFactory(){
		String[] nodes = env.getProperty("spring.redis.cluster.nodes").split(",");
		
		RedisClusterConfiguration configuration = new RedisClusterConfiguration(Arrays.asList(nodes));
		JedisPoolConfig config = setConfig();
		JedisConnectionFactory factory = new JedisConnectionFactory(configuration,config);
		return factory;
	}
	private JedisPoolConfig setConfig() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(1000);//最大实例数
		config.setMinIdle(10);//最大空闲实例数
		config.setMinIdle(1);//最小空闲实例数
		config.setMaxWaitMillis(30000);//获取实例等待时间
		config.setTestOnBorrow(true);//获取实例时，校验对象有效，无效则重新选择
		config.setTestOnReturn(true);//归还给连接池时，校验对象有效性
		config.setTestWhileIdle(true);//是否对空闲实例校验有效性，TimeBetweenEvictionRunsMillis为正数是有效
		config.setTimeBetweenEvictionRunsMillis(1);
		return config;
	}
	
	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
		StringRedisTemplate template = new StringRedisTemplate(factory);
		Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
		ObjectMapper om = new ObjectMapper();		
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		serializer.setObjectMapper(om);
		template.setValueSerializer(serializer);
		template.afterPropertiesSet();
		String key = "";
		String value = "";
		template.opsForValue().set(key, value);
		Map<String, String> m = new HashMap<String, String>();
		template.opsForHash().putAll(key, m);
		return template;
	}
	
	

}
